//
//  GQHPushFreightsView.m
//  Xanthium
//
//  Created by GuanQinghao on 07/03/2018.
//  Copyright © 2018 GuanQinghao. All rights reserved.
//

#import "GQHPushFreightsView.h"
#import "GQHHeader.h"
#import "GQHCustomCell.h"
#import "GQHFreightModel.h"

@interface GQHPushFreightsView () <UITableViewDelegate,UITableViewDataSource,GQHFreightCellDelegate>


@end

@implementation GQHPushFreightsView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        [self loadLocalData];
        [self loadUserInterface];
    }
    return self;
}

- (void)loadLocalData {
    
}

- (void)loadUserInterface {
    
    _qh_tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    _qh_tableView.delegate = self;
    _qh_tableView.dataSource = self;
    _qh_tableView.backgroundColor = kWhiteColor;
    _qh_tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    _qh_tableView.showsVerticalScrollIndicator = NO;
    if (@available(iOS 11.0, *)) {
        
        _qh_tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    [self addSubview:_qh_tableView];
    [_qh_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self).with.inset(self.qh_statusBarHeight + self.qh_navigationBarHeight);
        make.left.and.right.mas_equalTo(self);
        make.bottom.mas_equalTo(self.mas_bottom);
    }];
}

#pragma mark --Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (0 == section) {
        
        return 1;
    } else {
        
        return _qh_freightModelArray.count;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.section) {
        
        return 80.0f;
    } else {
        
        return 80.0f;
    } 
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (0 == indexPath.section) {
        
        static NSString *identifier = @"aCell";
        GQHFollowedCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHFollowedCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.userInteractionEnabled = NO;
        }
        
        [cell.qh_avatarImageView sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@",[NSString qh_imageURLString],_qh_truckModel.qh_driverAvatar]] placeholderImage:[UIImage qh_imageWithColor:kLightGrayColor]];
        cell.qh_nameLabel.text = _qh_truckModel.qh_driverName;
        cell.qh_subtitleLabel.text = _qh_truckModel.qh_licensePlateNumber;
        cell.qh_contentLabel.text = [NSString stringWithFormat:@"%@/%@/%@吨",_qh_truckModel.qh_normalLength,_qh_truckModel.qh_model,_qh_truckModel.qh_normalLoading];
        cell.qh_followed = _qh_truckModel.qh_followed;
        
        return cell;
    } else {
        
        static NSString *identifier = @"bCell";
        GQHFreightCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell) {
            
            cell = [[GQHFreightCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.qh_routeLabel.font = kBiggerFont;
            cell.qh_contentLabel.font = kSmallerFont;
            cell.qh_timeLabel.font = kSmallerFont;
        }
        cell.qh_delegate = self;
        cell.qh_pushButton.tag = indexPath.row;
        
        GQHFreightModel *freightModel = _qh_freightModelArray[indexPath.row];
        
        cell.qh_routeLabel.text = [NSString stringWithFormat:@"%@-%@",freightModel.qh_departure,freightModel.qh_destination];
        cell.qh_routeLabel.text = [NSString stringWithFormat:@"%@-%@",freightModel.qh_departure,freightModel.qh_destination];
        cell.qh_contentLabel.text = [NSString stringWithFormat:@"%@/%.0f吨/%@/%@",freightModel.qh_freightName,freightModel.qh_freightWeight,freightModel.qh_modelWanted,freightModel.qh_lengthWanted];
        cell.qh_timeLabel.text = [NSString stringWithFormat:@"装车时间:%@",freightModel.qh_loadingTime];
        if ([freightModel.qh_pushOrNot isEqualToString:@"02"]) {
            
            [cell.qh_pushButton setTitle:@"推送货源" forState:UIControlStateNormal];
            cell.qh_pushButton.userInteractionEnabled = YES;
        } else {
            
            [cell.qh_pushButton setTitle:@"已推送" forState:UIControlStateNormal];
            cell.qh_pushButton.userInteractionEnabled = NO;
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    if (1 == section) {
        
        return 40.0f;
    } else {
        
        return CGFLOAT_MIN;
    }
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = kLightGrayColor;
    
    if (1 == section) {
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.text = @"我的货源";
        titleLabel.textAlignment = NSTextAlignmentCenter;
        [headerView addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.and.left.and.bottom.and.right.mas_equalTo(headerView);
        }];
    }
    
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    return CGFLOAT_MIN;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    
    return [UIView new];
}

- (void)qh_pushFreight:(UIButton *)sender {
    
    if ([_qh_delegate respondsToSelector:@selector(qh_pushFreight:)]) {
        
        [_qh_delegate qh_pushFreight:sender];
    }
}

@end
